<template>
  <div class="page-box-height">
    <div class="page-container">
      <div class="title">
        <span>预览页面</span>
        <span class="equipment-icon-box">
          终端：
          <svg-icon class="equipment-icon" :class="{active: designDeviceChoose == 'PC'}" icon-class="pc" title="PC" @click.native="designDeviceClick('PC')"></svg-icon>
          <svg-icon class="equipment-icon" :class="{active: designDeviceChoose == 'APP'}" icon-class="APP" title="APP" @click.native="designDeviceClick('APP')"></svg-icon>
        </span>
      </div>
      <div class="form-main">
        <div :class="designDeviceChoose==='APP'?'APP-design':'PC-design'">
          <view-template v-if="viewTemplateShow" :initTemplateInfo="initTemplateInfo[designDeviceChoose]" ref="viewTemplate"></view-template>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import viewTemplate from './viewTemplate/viewTemplate'
export default {
  name: 'preview',
  components: {viewTemplate},
  data() {
    return {
      initTemplateInfo: [],
      viewTemplateShow: false,
      designDeviceChoose: 'PC',
      designDevice: ['APP', 'PC'],
    }
  },
  created() {
    this.initSortable()
    this.refreshFnForStorage = data => {
      if (data.key === 'initTemplateInfo') {
        this.initSortable()
      }
    }
    window.addEventListener('storage', this.refreshFnForStorage)
  },
  beforeDestroy() {
    window.removeEventListener('storage', this.refreshFnForStorage)
  },
  methods: {
    designDeviceClick(equipment) {
      this.designDeviceChoose = equipment
      this.viewTemplateShow = false
      this.$nextTick(() => {
        this.viewTemplateShow = true
      })
    },
    initSortable() {
      this.initTemplateInfo = JSON.parse(localStorage.getItem('initTemplateInfo'))
      this.viewTemplateShow = false
      this.$nextTick(() => {
        this.viewTemplateShow = true
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.page-container {
  background: white;
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 10px;

  .equipment-icon-box {
    display: inline-block;
    margin-left: 10px;
    color: #999;
    font-weight: 400;

    &::before {
      content: "";
      display: inline-block;
      height: 20px;
      border-left: 2px solid #ccc;
      margin-right: 10px;
      transform: translateY(3px);
    }

    .equipment-icon {
      margin-right: 8px;
      cursor: pointer;

      &.active {
        color: #477fe3;
      }
    }
  }

  .title {
    height: 30px;
    line-height: 30px;
    font-size: 16px;
    font-weight: 600;
  }

  .form-main {
    flex: 1;
    border: 1px solid #ddd;
    margin: auto;
    width: 100%;
    padding: 10px;
    overflow-y: auto;

    .PC-design {
      width: 100%;
      height:100%;
    }

    .APP-design {
      background: url(../pageComponents/assets/phone.png) no-repeat;
      background-size: 100% 100%;
      width: 350px;
      height: 800px;
      margin: auto;
      padding: 106px 17px 99px 19px;
      box-sizing: border-box;
      transform: translateY(20px);
    }
  }
}
</style>
